<?php

namespace app\common\model\user;

use app\common\model\User;
use think\Model;


class Level extends Model
{





    // 表名
    protected $name = 'user_level';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [];







    /**
     * 获取上级等级
     */
    static function getNextLevel($all_level, $current_level = null)
    {
        if ($current_level) {
            foreach ($all_level as $v) {
                if ($v->last_month_ach > $current_level->last_month_ach) {
                    $has_user = User::where('level_id', $v->id)->value('id');
                    if ($has_user) {
                        return $v;
                    }
                }
            }
        } else {
            foreach ($all_level as $v) {
                $has_user = User::where('level_id', $v->id)->value('id');
                if ($has_user) {
                    return $v;
                }
            }
        }

        return null;
    }
}
